package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.entity.vo.UserAssetsVO;
import com.sc.nft.enums.CoinCategoryEnum;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserAssets;
import com.sc.nft.mapper.UserAssetsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * (sc_user_assets)数据DAO
 *
 * @author rdg
 * @since 2022-05-30 10:42:05
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class UserAssetsDao extends ServiceImpl<UserAssetsMapper, UserAssets> {
    @Resource
    private  UserAssetsMapper userAssetsMapper;

    public UserAssets getByUserIdAndCoinId(Long userId, Long code) {
        LambdaQueryWrapper<UserAssets> assetsWrapper = new LambdaQueryWrapper<>();
        assetsWrapper.eq(UserAssets::getUserId,userId).eq(UserAssets::getBlockChainId, CoinCategoryEnum.INTEGRAL.getCode());
        assetsWrapper.eq(UserAssets::getIsEnable,Boolean.TRUE);
        return getOne(assetsWrapper);
    }

    public UserAssets getOneByUserIdAndBlockChainId(Long userId, Long blockChainId) {
        LambdaQueryWrapper<UserAssets> assetsWrapper = new LambdaQueryWrapper<>();
        assetsWrapper.eq(UserAssets::getUserId,userId);
        assetsWrapper.eq(UserAssets::getBlockChainId,blockChainId);
        assetsWrapper.eq(UserAssets::getIsEnable,Boolean.TRUE);
        return getOne(assetsWrapper);
    }

    public Page<UserAssetsVO> pageUserAssetsList(String userUuid, String nickName, Long userId, int pageNo, int pageSize) {
      return userAssetsMapper.pageUserAssetsList(new Page(pageNo,pageSize),userUuid,nickName,userId);
    }

    public boolean addTokenNumByUserId(Long userId, BigDecimal controlsTokenNum) {
        LambdaUpdateWrapper<UserAssets> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserAssets::getUserId,userId);
        wrapper.setSql("token_num = token_num + "+controlsTokenNum);
        return update(new UserAssets(),wrapper);
    }

    public boolean subTokenNumByUserId(Long userId, BigDecimal tokenNum) {
        return userAssetsMapper.subTokenNumByUserId(userId,tokenNum);
    }
}